package java6703.lesson04;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;

//控制器类
public class ListFilter6703Controller {

    @FXML
    private Label lbl5;

    @FXML
    private ListView<?> lv5;

    @FXML
    private ListView<?> lvAll;

    @FXML
    void buttonClick(ActionEvent event) {
        ObservableList observableList = createList6703();
        //获取并显示20个20个[60,100]的整数
        lvAll.setItems(observableList);
        observableList = findList6703(observableList);
        //显示5的倍数的数据
        lv5.setItems(observableList);
    }

    //随机产生20个[60,100]的整数，存储到集合中
    public ObservableList<Integer> createList6703() {
        int num = 0;
        ListView<Integer> listView = new ListView<>();
        ObservableList<Integer> items = listView.getItems();
        for (int i = 0; i <= 20; i++) {
            num = (int) (Math.random() * 41 + 60);
            items.addAll(num);
        }
        return items;
    }

    //从一个集合中提取5的倍数的数据，放到另一个集合
    public ObservableList<Integer> findList6703(ObservableList<Integer> integers) {
        ListView<Integer> listView = new ListView<>();
        ObservableList<Integer> items = listView.getItems();

        //遍历integers集合
        for (int i = 0; i < integers.size(); i++) {

            //如果余数为零就加入items集合
            if (integers.get(i) % 5 == 0) {
                items.addAll(integers.get(i));
            }
        }
        return items;
    }
}
